package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.testng.annotations.DataProvider;

import com.example.fw.AppManager;
import com.example.tests.ContactObject;

public class ContactDataGenerator {
	private static AppManager manager;
	static Random rnd = new Random();
	public static String[] MONTHS = new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	
	public int generateRandomNumberBetween(int start, int end) {
		  return new Random().nextInt(end - start) + start;
	}
	
	@DataProvider(name = "createContacts")
	public static Iterator<Object[]> generateRandomContacts() {
		List<ContactObject> contacts = new ContactDataGenerator().generateRandomContactsList(5);
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactObject contact:contacts) {
			list.add(new Object[]{contact});	
		}
		return list.iterator();
	}

	@DataProvider(name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() throws IOException {
		List<Object[]> list = new ArrayList<Object[]>();
		BufferedReader reader = new BufferedReader (new FileReader(manager.getProperty("file_contactData")));
//		BufferedReader reader = new BufferedReader(new FileReader("contacts.dat"));
		String line = reader.readLine();
		while(line!=null){
			String[] parts = line.split("\t");

			ContactObject contact = new ContactObject()
			.setFirstName(parts[0])
			.setLastName(parts[1])
			.setAddress(parts[2])
			.setTelHome(parts[3])
			.setTelMobile(parts[4])
			.setTelWork(parts[5])
			.setBirthDay(parts[6])
			.setBirthMonth(parts[7])
			.setBirthYear(parts[8])
			.setGroup(parts[9])
			.setEmail(parts[10])
			.setEmail2(parts[11])
			.setAddress2(parts[12])
			.setPhone2(parts[13]);

			list.add(new ContactObject[]{contact});
			line = reader.readLine();
		}
		return list.iterator();
	}
	public static void main(String[] args){
		if (args.length < 2){
			System.out.println("Specify two parameters: file & count");
			return; 
		}
		String file = args[0];
		int count = Integer.parseInt(args[1]); 
		try {
			new ContactDataGenerator().generateDataToFile(file, count);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

	private void generateDataToFile(String fileName, int count) throws IOException {
		List<ContactObject> contacts = generateRandomContactsList(count);
		File file = new File(fileName);
		if (file.exists()) {
			System.out.println("File exists. Stop generator");
			return;
		}
		FileWriter writer = new FileWriter(file);
		for (ContactObject contact:contacts){
			writer.write(contact.firstname + "\t"+ contact.lastname + "\t" + contact.address+"\t" + contact.telhome+"\t" + contact.telmobile+"\t" + contact.telwork+"\t" + contact.birthday+"\t" + contact.birthmonth+"\t" + contact.birthyear+"\t" + contact.group+"\t" + contact.email+"\t" + contact.email2+"\t" + contact.address2+"\t" + contact.phone2+"\t" + "\n");
		}
		writer.close();
	}

	private List<ContactObject> generateRandomContactsList(int count) {
		List<ContactObject> list = new ArrayList<ContactObject>();
		for (int i = 0; i < count; i++) {
			ContactObject contact = new ContactObject()
			.setFirstName("firstname"+rnd.nextInt())
			.setLastName("lastname"+rnd.nextInt())
			.setAddress("address"+rnd.nextInt())
			.setTelHome("telhome"+rnd.nextInt())
			.setTelMobile("telmobile"+rnd.nextInt())
			.setTelWork("telwork"+rnd.nextInt())
			.setBirthDay("" + generateRandomNumberBetween(1,30))
			.setBirthMonth(MONTHS[generateRandomNumberBetween(1, 12)])
			.setBirthYear("" + generateRandomNumberBetween(1950, 2013))
			.setGroup("[none]")
			.setEmail("email"+rnd.nextInt())
			.setEmail2("email2"+rnd.nextInt())
			.setAddress2("address2"+rnd.nextInt())
			.setPhone2("phone2"+rnd.nextInt());

			list.add(contact);
		}
		return list;
	}
}
